import React from 'react';
import store from "../store";
import {addCart} from "../store/slice/cart";
import {useNavigate} from "react-router-dom";

function GoodsList(props) {
    const navigate = useNavigate()
    return (
        <div>
            {
                !store.getState().goods.goodsList.length? <h3>暂无商品，快去选购吧~</h3>:store.getState().goods.goodsList.map(list=>(
                <div key={list.id}>
                <h3>商品名称：{list.goodsName}</h3>
                <h3>商品价格：￥{list.goodsPrice.toFixed(2)}</h3>
                <button onClick={()=>{
                store.dispatch(addCart(list))
                navigate('/cart')
            }}>加入购物车</button>
                    <hr/>
                </div>
                ))
            }
        </div>
    );
}

export default GoodsList;